﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using AutoResumeSend.Entity;
using AutoResumeSend.DAC;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
using System.Windows.Forms;
namespace AutoResume
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtSubject.Text = "";
            this.txtTargetEmail.Text = "";
            this.txtUrl.Text = "";
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
           
            List<SourceMailInfo> list = SourceMailList.GetSourceMailList();

            if (list != null)
            {
                for (int index = 0; index < list.Count; index++)
                {
                    chklstSourceEmail.Items.Add(list[index].DisplayName);
                }
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (chklstSourceEmail.SelectedItems.Count < 1)
            {
                MessageBox.Show("请选择发送人!", "提示信息", MessageBoxButtons.OK);
                return;
            }

            if(string.IsNullOrEmpty(txtTargetEmail.Text.ToString()))
            {
                 MessageBox.Show("请输入邮件地址!", "提示信息", MessageBoxButtons.OK);
                return;
            }

             if(string.IsNullOrEmpty(txtSubject.Text.ToString()))
            {
                 MessageBox.Show("请输入主题!", "提示信息", MessageBoxButtons.OK);
                return;
            }

            try
            {
                AutoResumeSend.Componments.SqlManager sqlMgr = new AutoResumeSend.Componments.SqlManager();
                bool isExistsEmail = sqlMgr.isExistEmail(txtTargetEmail.Text.ToString());
                if (isExistsEmail)
                {
                    if (MessageBox.Show("邮件已经发送过,是否需要重复发送?", "提示信息", MessageBoxButtons.YesNo) == DialogResult.No)
                        return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("检查邮件是否存在失败!"+ex.ToString(), "提示信息", MessageBoxButtons.OK);
                btnSend.Enabled = true;
                return;
            }

            btnSend.Enabled = false;
            List<SourceMailInfo> list = SourceMailList.GetSourceMailList();
            for (int index = 0; index < chklstSourceEmail.CheckedItems.Count; index++)
            {
                try
                {
                    SourceMailInfo info = list.FindAll(delegate(SourceMailInfo m)
                    {
                        return m.DisplayName == chklstSourceEmail.CheckedItems[index].ToString();
                    })[0];

                    //发送邮件
                    MailInfo mail = new MailInfo();
                    mail.Subject = txtSubject.Text.ToString();

                    mail.From = info.MailName;
                    mail.To = txtTargetEmail.Text.ToString();
                    mail.FromDisplayName = info.DisplayName;
                    string attchmentsPath = info.AttchmentsPath;
                    if (string.IsNullOrEmpty(attchmentsPath))
                    {
                        attchmentsPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "resume.doc");
                    }
                    mail._attachmentPath = attchmentsPath;
                    string bodyPath = info.BodyPath;
                    if (string.IsNullOrEmpty(bodyPath))
                    {
                        bodyPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "resume.txt");
                    }
                    string body = File.ReadAllText(bodyPath,System.Text.Encoding.UTF8);
                  
                    mail.Body = body;
                    mail.Body = Regex.Replace(mail.Body, "\r\n", "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", RegexOptions.IgnoreCase);
                    mail.Body = Regex.Replace(mail.Body, "\n", "<br>", RegexOptions.IgnoreCase);
                    AutoResumeSend.Componments.SendMailManager mailMgr = new AutoResumeSend.Componments.SendMailManager();
                    mailMgr.sendMail(mail, info.Host, info.Port, info.AdminName, info.Password);

                    //添加数据库
                    AutoResumeSend.Componments.SqlManager sqlMgr = new AutoResumeSend.Componments.SqlManager();
                    AutoResumeSend.Entity.WebSiteInfo catchedWeb = new WebSiteInfo();
                    catchedWeb.CompanyEmail = txtTargetEmail.Text.ToString();
                    catchedWeb.OccurUrl = txtUrl.Text.ToString();
                    catchedWeb.WebId = 1;
                    sqlMgr.AddResumeSendInfo(catchedWeb);
                    MessageBox.Show("发送成功!");
                    btnSend.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("发送失败,异常：【{0}】", ex));
                    btnSend.Enabled = true;
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Application.Exit();
            // Speech();
            AutoResumeSend.Componments.SpeechManager.instance().AnalyseSpeak("我是中国人");
        }

        public  void Speech()
        {
            //参考文档的起始链接在:
            //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/SAPI51sr/html/ispvoice_speak.asp

            // 引擎COM对象.
            SpeechLib.SpVoiceClass sp = new SpeechLib.SpVoiceClass();

            // 朗读的内容到一个文件流里去.
            SpeechLib.SpFileStreamClass fs = new SpeechLib.SpFileStreamClass();
            //设定流的格式, 48kHZ, 16Bit, 立体声. 如果是电话音质, 8K8bit单声道就可以了.
            fs.Format.Type = SpeechLib.SpeechAudioFormatType.SAFT48kHz16BitStereo;
            fs.Open("c:\\test.wav", SpeechLib.SpeechStreamFileMode.SSFMCreateForWrite, false);
            sp.AudioOutputStream = fs;    // 如果不指定这个内容, 那就会在计算机的声卡上放出来.
            // The format of selection criteria is 
            //"Attribute = Value" and "Attribute != Value." 
            // Voice attributes include 
            //"Gender," "Age," "Name," "Language," and "Vendor."
            // 上述这些属性的值可以从注册表的
            //HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\MSSimplifiedChineseVoice\Attributes
            // 看到

            // 得到属性符合指定要求的所有的拼读token. "Language = 409"表示得到所有的英文拼读器. 等号两边的空格不可少! 
            // 如果有多个过滤条件要指定, 中间用分号分开, 如要指定英文女声, 则为
            // "Language = 409; Gender = Female"
            SpeechLib.ISpeechObjectTokens sps = sp.GetVoices("Language = 409", "");
            // 可能有多个拼读器. 咱们每个都用一遍,  是不同的嗓音, 有男有女.
            for (int i = 0; i < sps.Count; i++)
            {
                sp.Voice = sps.Item(i);    //这里这个圆括号不是我写错了, 就是这样的.这是一个方法,不是索引器.

                sp.Speak("User experience and interface design in the context of creating software represents an approach that puts the user, rather than the system, at the center of the process.",
                    SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault);
                // 可以用标志位指定异步调用这个方法. 
            }

            // 得到中文拼读器, 因为目前只有一个, 也就不用for了.
            sps = sp.GetVoices("Language = 804", "");
            if (sps.Count > 0)
            {
                sp.Voice = sps.Item(0);
                sp.Speak("中华人民共和国, 中央人民政府, 成立了!.",
                    SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault);
            }
            fs.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(sp);
        }

    }
}
